package com.traffic.controller;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.traffic.commons.base.BaseController;
import com.traffic.commons.utils.PageInfo;
import com.traffic.commons.utils.DateUtils;
import com.traffic.model.TrafficLawLogs;
import com.traffic.service.ITrafficLawLogsService;

/**
 * <p>
 * 违法记录表  前端控制器
 * </p>
 * @author Hongyue Wang
 * @since 2017-04-22
 */
@Controller
@RequestMapping("/trafficLawLogs")
public class TrafficLawLogsController extends BaseController{
    
    @Autowired private ITrafficLawLogsService trafficLawLogsService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/trafficLawLogs/trafficLawLogsList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TrafficLawLogs trafficLawLogs, Integer page, Integer rows, String sort,String order) {
        trafficLawLogs.setIsDelete("N");
        EntityWrapper<TrafficLawLogs> ew = new EntityWrapper<TrafficLawLogs>();
        ew.setEntity(trafficLawLogs);
        Page<TrafficLawLogs> pages = getPage(page, rows, sort, order);
        pages = trafficLawLogsService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/trafficLawLogs/trafficLawLogsAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(TrafficLawLogs trafficLawLogs) {
        trafficLawLogs.setCreatTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficLawLogs.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficLawLogs.setIsDelete("N");
        boolean b = trafficLawLogsService.insert(trafficLawLogs);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TrafficLawLogs trafficLawLogs = new TrafficLawLogs();
        trafficLawLogs.setId(id.intValue());
        trafficLawLogs.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficLawLogs.setIsDelete("Y");
        boolean b = trafficLawLogsService.updateById(trafficLawLogs);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TrafficLawLogs trafficLawLogs = trafficLawLogsService.selectById(id);
        model.addAttribute("trafficLawLogs", trafficLawLogs);
        return "admin/trafficLawLogs/trafficLawLogsEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(TrafficLawLogs trafficLawLogs) {
        trafficLawLogs.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        boolean b = trafficLawLogsService.updateById(trafficLawLogs);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
    
}
